package com.zhengxyou.demoscrolltab

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import com.zhengxyou.demoscrolltab.databinding.FragmentTextBinding

class TextFragment : BaseFragment() {
    private var _binding: FragmentTextBinding? = null
    private var _title: String? = null
    private val title get() = _title
    private val binding
        get() = _binding!!

    override fun scrollCallback(scrollY: Int, dy: Int) {
        (requireActivity() as MainActivity).scrollCallback(scrollY, dy)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _title = arguments?.getString("title")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentTextBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        binding.tvTitle.text = title
        val layoutParams = binding.llContent.layoutParams as FrameLayout.LayoutParams
        layoutParams.topMargin = (requireActivity() as MainActivity).getTopHeight()

        binding.nsScroll.setOnScrollChangeListener(
            //上一次停止后垂直滑动的距离//当前垂直滑动的距离,头部的时候是0，当前滚动条的位置
            NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
                scrollCallback(
                    scrollY,
                    scrollY - oldScrollY
                )
            })
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        fun newInstance(title: String): Fragment {
            val args = Bundle()
            args.putString("title", title)
            val fragment = TextFragment()
            fragment.arguments = args
            return fragment
        }
    }
}